package srx.open.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.listener.SaveListener;
import srx.open.com.R;
import srx.open.com.bean.CoursePublish;
import srx.open.com.bean.User;
import srx.open.com.mode.AddrItem;
import srx.open.com.mode.ClassItem;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;
import srx.open.com.utils.UserUtils;
import srx.open.com.widgets.ActionSheet;

/**
 * 课程发布界面.
 * ----------------------
 * 首先需要获取定位.
 * 根据定位的地址线提前显示在地区上，
 * 如果定位失败则显示无.
 * ------------------------
 *
 * @author hailongqiu 356752238@qq.com
 */
public class PublishCourseActivity extends ExtendBaseActivity {

    private static final String TAG = "PublishCourseActivity";

    public static final int TEACH_STUDENT_STATE = 0; // 教学生
    public static final int FIND_TEACHER_STATE = 1;   // 寻老师

    // 标题栏
    @Bind(R.id.left_lay)
    View left_lay;
    @Bind(R.id.right_lay)
    View right_lay;
    @Bind(R.id.left_tv)
    TextView left_tv;
    @Bind(R.id.right_tv)
    TextView right_tv;

    // 课程分类
    @Bind(R.id.course_name_tv)
    TextView course_name_tv;
    // 地区
    @Bind(R.id.addr_name_tv)
    TextView addr_name_tv;
    //  需求类型
    @Bind(R.id.type_name_tv)
    TextView type_name_tv;
    // 发布需求的标题
    @Bind(R.id.title_et)
    EditText title_et;
    // 发布需求的内容
    @Bind(R.id.content_et)
    EditText content_et;

    //
    private int typeState = TEACH_STUDENT_STATE;

    @Override
    public void initAllDatas() {
    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_publish_course);
        ButterKnife.bind(this);
        if (typeState == TEACH_STUDENT_STATE) {
            type_name_tv.setText("教学生");
        } else {
            type_name_tv.setText("寻老师");
        }
        initTitles();
    }

    private void initTitles() {
        left_tv.setText("发布需求");
        right_tv.setText("发布");
        addr_name_tv.setText("正在定位位置...");
        addrLocationHandler.sendEmptyMessageDelayed(0, 1000);
    }

    Handler addrLocationHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (mSrxApplication.locationProvinceItem != null) {
                addr_name_tv.setText(mSrxApplication.locationProvinceItem.getName());
                if (mSrxApplication.locationCityItem != null) {
                    addr_name_tv.setText(mSrxApplication.locationCityItem.getName());
                    if (mSrxApplication.locationDistrictItem != null) {
                        addr_name_tv.setText(mSrxApplication.locationDistrictItem.getName());
                    }
                }
            }
            if (TextUtils.isEmpty(addr_name_tv.getText().toString()))
                addr_name_tv.setText("无");
        }
    };

    /**
     * 发布需求.
     */
    @OnClick(R.id.right_lay)
    void rightLayOnClick(View view) {
        String titleName = title_et.getText().toString();
//        User user = UserUtils.getCurrentUser(mContext);
//        if (null == user) {
//            OpenToast.showMsg("需要先登录才能使用此功能");
//           return;
//        }
        if (TextUtils.isEmpty(titleName)) {
            OpenToast.showMsg("标题栏必填");
            return;
        }
        //
        String content = content_et.getText().toString();
        CoursePublish coursePublish = new CoursePublish();
        coursePublish.setTitle(titleName);
        coursePublish.setContent(content);
        // 课程分类设置.
        Log.w(TAG + " OneId:" + classItemLists.get(0).getOneId());
        Log.w(TAG + " TwoId:" + classItemLists.get(1).getTwoId());
        Log.w(TAG + " ThreeId:" + classItemLists.get(2).getThreeId());
        coursePublish.setOneId(classItemLists.get(0).getOneId());
        coursePublish.setTwoId(classItemLists.get(1).getTwoId());
        coursePublish.setThreeId(classItemLists.get(2).getThreeId());
        //  地区设置
        if (addrItemLists.get(0).getId() != -1) { // 手动选择.
            coursePublish.setProvinceId(addrItemLists.get(0).getId());
            if (addrItemLists.get(1).getId() != -1) {
                coursePublish.setCityId(addrItemLists.get(1).getId());
                if (addrItemLists.get(2).getId() != -1) {
                    coursePublish.setDistrictId(addrItemLists.get(2).getId());
                }
            }
        } else { // 定位更新.
            if (mSrxApplication.locationProvinceItem != null) {
                coursePublish.setProvinceId(mSrxApplication.locationProvinceItem.getId());
                if (mSrxApplication.locationCityItem != null) {
                    coursePublish.setCityId(mSrxApplication.locationCityItem.getId());
                    if (mSrxApplication.locationDistrictItem != null) {
                        coursePublish.setDistrictId(mSrxApplication.locationDistrictItem.getId());
                    }
                }
            }
        }
        // 定位设置
        if (mSrxApplication.locationBmobGeoPoint != null) {
            coursePublish.setLocation(mSrxApplication.locationBmobGeoPoint);
        } else { // 如果没有定位，去获取以前的定位数据. 或者.... ...
            // ... ...
        }
        // 需求类型
        coursePublish.setType(typeState);
        // 关联用户
        User user = UserUtils.getCurrentUser(mContext);
        if (user != null) {
            coursePublish.setUsertt(user); // 关联发布的用户.
        }
        coursePublish.save(mContext, new SaveListener() {
            @Override
            public void onSuccess() {
                OpenToast.showMsg("发布成功");
                finish();
            }

            @Override
            public void onFailure(int state, String msg) {
                OpenToast.showMsg("发布失败");
            }
        });
    }

    /**
     * 课程分类选择.
     */
    @OnClick(R.id.course_rlay)
    void courseRlayOnClick(View view) {
        Intent intent = new Intent(PublishCourseActivity.this, CourseClassActivity.class);
        intent.putExtra("selectClass", course_name_tv.getText());
        startActivityForResult(intent, CourseClassActivity.RESULT_CLASS_STATE);
    }

    /**
     * 地区城市选择.
     */
    @OnClick(R.id.addr_rlay)
    void addrLayOnClick(View view) {
        Intent intent = new Intent(PublishCourseActivity.this, AddrActivity.class);
        intent.putExtra("selectAddr", addr_name_tv.getText());
        startActivityForResult(intent, AddrActivity.RESULT_ADDR_STATE);
    }

    private ArrayList<AddrItem> addrItemLists = new ArrayList<AddrItem>() {
        {
            add(new AddrItem()); // addr1 比如 四川省
            add(new AddrItem()); // addr2 比如 成都市
            add(new AddrItem()); // addr3 比如 武侯区
        }
    };
    private ArrayList<ClassItem> classItemLists = new ArrayList<ClassItem>() {
        {
            add(new ClassItem()); // 分类一
            add(new ClassItem()); // 分类二
            add(new ClassItem()); // 分类三
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;
        Bundle b = data.getExtras();
        switch (resultCode) {
            case AddrActivity.RESULT_ADDR_STATE: // 地区返回值.
                if (b == null)
                    return;
                // addr3 比如武侯区
                String str3 = b.getString("addr3");
                int id3 = b.getInt("id3");
                addrItemLists.get(2).setId(id3);
                addrItemLists.get(2).setName(str3);
                // addr2 比如 成都市
                String str2 = b.getString("addr2");
                int id2 = b.getInt("id2");
                addrItemLists.get(1).setId(id2);
                addrItemLists.get(1).setName(str2);
                // addr1 比如 四川省
                String str1 = b.getString("addr1");
                int id1 = b.getInt("id1");
                addrItemLists.get(0).setId(id1);
                addrItemLists.get(0).setName(str1);
                // 显示最后选择的地区.
                String selectAddr = b.getString("selectAddr");
                addr_name_tv.setText(selectAddr);
                break;
            case CourseClassActivity.RESULT_CLASS_STATE: // 课程分类返回值.
                if (b == null)
                    return;
                // 设置分类三.
                String class3 = b.getString("class3");
                int classId3 = b.getInt("id3");
                classItemLists.get(2).setThreeId(classId3);
                classItemLists.get(2).setDescription(class3);
                // 设置分类二.
                String class2 = b.getString("class2");
                int classId2 = b.getInt("id2");
                classItemLists.get(1).setTwoId(classId2);
                classItemLists.get(1).setDescription(class2);
                // 设置分类一.
                String class1 = b.getString("class1");
                int classId1 = b.getInt("id1");
                classItemLists.get(0).setOneId(classId1);
                classItemLists.get(0).setDescription(class1);
                // 显示最后选择的地区.
                String selectClass = b.getString("selectClass");
                course_name_tv.setText(selectClass);
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.left_lay)
    void leftLayOnClick(View view) {
        finish();
    }

    /**
     * 需求类型选择 (教学生/寻老师)
     */
    @OnClick(R.id.class_type_rlay)
    void classTypeRlayOnClick(View view) {
        setTheme(R.style.ActionSheetStyleIOS7);
        ActionSheet
                .createBuilder(PublishCourseActivity.this,
                        getSupportFragmentManager())
                .setCancelButtonTitle("取消")
                .setOtherButtonTitles("教学生", "寻老师")
                .setCancelableOnTouchOutside(true)
                .setListener(new ActionSheet.ActionSheetListener() {
                    @Override
                    public void onOtherButtonClick(ActionSheet actionSheet,
                                                   int index) {
                        switch (index) {
                            case TEACH_STUDENT_STATE: // 教学生.
                                type_name_tv.setText("教学生");
                                typeState = TEACH_STUDENT_STATE;
                                break;
                            case FIND_TEACHER_STATE: // 寻找老师
                                type_name_tv.setText("寻老师");
                                typeState = FIND_TEACHER_STATE;
                                break;
                        }
                    }

                    @Override
                    public void onDismiss(ActionSheet actionSheet,
                                          boolean isCancel) {
                    }
                }).show();
    }


}
